package com.activeMQ.property;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jms.Message;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.activemq.command.ActiveMQMapMessage;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.activeMq.receiver.ITopicMsgHandler;
import com.orangelife.service.cerHouseholder.ICerHouseholderService;
import com.orangelife.service.cerHouseholder.model.CerHouseholderDto;
import com.orangelife.service.propFeeApartment.IPropFeeApartmentService;
import com.orangelife.service.propFeeApartment.model.PropFeeApartmentDto;
import com.orangelife.service.propFeeBuilding.IPropFeeBuildingService;
import com.orangelife.service.propFeeBuilding.model.PropFeeBuildingDto;
import com.orangelife.service.propFeeUnit.IPropFeeUnitService;
import com.orangelife.service.propFeeUnit.model.PropFeeUnitDto;

/***
 * jian
 * 
 * @author shendiao
 * 
 */
public class Property implements ITopicMsgHandler {
	private static Logger logger = Logger.getLogger(Property.class);

	/***
	 * 监听产权的变动信息
	 */
	@Override
	public void onMsgRecv(Message message) throws Exception {

		try {

			/***
			 * 获取spring 的上下文
			 */
			WebApplicationContext wac = ContextLoader
					.getCurrentWebApplicationContext();
			if (wac != null) {
				/***
				 * 初始化实例 业主关系
				 */
				ICerHouseholderService cerHouseholderService = (ICerHouseholderService) wac
						.getBean("cerHouseholderService");

				/**
				 * 初始化房屋实例
				 */
				IPropFeeApartmentService propFeeApartmentService = (IPropFeeApartmentService) wac
						.getBean("propFeeApartmentService");

				/***
				 * 初始化楼栋
				 */
				IPropFeeBuildingService propFeeBuildingService = (IPropFeeBuildingService) wac
						.getBean("propFeeBuildingService");

				/***
				 * 初始化单元
				 */
				IPropFeeUnitService propFeeUnitService = (IPropFeeUnitService) wac
						.getBean("propFeeUnitService");
				/***
				 * 获取接收的消息
				 */
				ActiveMQMapMessage activeMQMapMessage = (ActiveMQMapMessage) message;

				/***
				 * 获取jms类型
				 */
				String messageType = activeMQMapMessage.getJMSType();
				/***
				 * 获取接收者类型
				 */
				Map<String, Object> receiver = activeMQMapMessage
						.getProperties();

				/***
				 * 获取发送的内容
				 */
				Map<String, Object> paMap = activeMQMapMessage.getContentMap();
				if (receiver.containsKey("receiver")
						&& !StringUtils.isBlank(messageType)) {
					if (paMap != null && paMap.size() > 0) {

						/***
						 * 新增产权
						 */
						if (receiver.get("receiver").toString()
								.equals("RECEIVER_ADD_PROPERTY")
								&& messageType.equals("RECEIVER_ADD_PROPERTY")) {

							if (paMap.containsKey("propertyInfo")) {
								JSONArray propertyInfo = JSONArray
										.fromObject(paMap.get("propertyInfo")
												.toString());

								for (int i = 0; i < propertyInfo.size(); i++) {
									PropFeeBuildingDto propFeeBuilding = new PropFeeBuildingDto();
									PropFeeUnitDto propFeeUnit = new PropFeeUnitDto();
									PropFeeApartmentDto propFeeApartment = new PropFeeApartmentDto();

									/***
									 * 添加楼栋
									 */
									JSONObject cmmBuilding = propertyInfo
											.getJSONObject(i).getJSONObject(
													"cmmBuilding");
									propFeeBuilding
											.setBuildingID(cmmBuilding
													.get("buildingID") != null ? cmmBuilding
													.getString("buildingID")
													: null);

									propFeeBuilding
											.setCommID(cmmBuilding
													.get("commID") != null ? cmmBuilding
													.getString("commID") : null);

									propFeeBuilding
											.setBuildingName(cmmBuilding
													.get("buildingName") != null ? cmmBuilding
													.getString("buildingName")
													: null);
									propFeeBuilding
											.setBuildingNameOther(cmmBuilding
													.get("buildingNameOther") != null ? cmmBuilding
													.getString("buildingNameOther")
													: null);

									propFeeBuilding
											.setBuildingSort(cmmBuilding
													.get("buildingSort") != null ? Integer.parseInt(cmmBuilding
													.getString("buildingSort"))
													: null);

									propFeeBuilding
											.setBuildingType(cmmBuilding
													.get("buildingType") != null ? cmmBuilding
													.getString("buildingType")
													: null);

									propFeeBuilding
											.setStatus(cmmBuilding
													.get("status") != null ? cmmBuilding
													.getString("status") : null);

									propFeeBuildingService
											.addPropFeeBuilding(propFeeBuilding);

									/***
									 * 添加单元
									 */
									JSONObject cmmUnit = propertyInfo
											.getJSONObject(i).getJSONObject(
													"cmmUnit");

									propFeeUnit
											.setBuildingID(cmmUnit
													.get("buildingID") != null ? cmmUnit
													.getString("buildingID")
													: null);
									propFeeUnit
											.setUnitID(cmmUnit.get("unitID") != null ? cmmUnit
													.getString("unitID") : null);
									propFeeUnit.setUnitName(cmmUnit
											.get("unitName") != null ? cmmUnit
											.getString("unitName") : null);
									propFeeUnitService
											.addPropFeeUnit(propFeeUnit);

									/***
									 * 添加房屋
									 */

									JSONObject cmmApartments = propertyInfo
											.getJSONObject(i).getJSONObject(
													"cmmApartments");

									propFeeApartment
											.setApartmentID(cmmApartments
													.get("apartmentID") != null ? cmmApartments
													.getString("apartmentID")
													: null);

									propFeeApartment
											.setApartmentNum(cmmApartments
													.get("apartmentNumb") != null ? cmmApartments
													.getString("apartmentNumb")
													: null);

									propFeeApartment
											.setApartmentType(cmmApartments
													.get("apartmentType") != null ? cmmApartments
													.getString("apartmentType")
													: null);

									propFeeApartment
											.setBuildingID(cmmApartments
													.get("buildingID") != null ? cmmApartments
													.getString("buildingID")
													: null);
									propFeeApartment
											.setFeeArea(cmmApartments
													.get("feeArea") != null ? cmmApartments
													.getString("feeArea")
													: null);

									propFeeApartment
											.setBuildingArea(cmmApartments
													.get("buildingArea") != null ? cmmApartments
													.getString("buildingArea")
													: null);

									propFeeApartment
											.setPropertyArea(cmmApartments
													.get("propertyArea") != null ? cmmApartments
													.getString("propertyArea")
													: null);
									propFeeApartment
											.setFloor(cmmApartments
													.get("floor") != null ? cmmApartments
													.getString("floor") : null);
									propFeeApartment
											.setIsDeliver(cmmApartments
													.get("isDeliver") != null ? cmmApartments
													.getString("isDeliver")
													: null);

									propFeeApartment
											.setIsOccupy(cmmApartments
													.get("isOccupy") != null ? cmmApartments
													.getString("isOccupy")
													: null);
									propFeeApartment
											.setUnitID(cmmApartments
													.get("unitID") != null ? cmmApartments
													.getString("unitID") : null);
									propFeeApartment
											.setSourceID(cmmApartments
													.get("sourceID") != null ? cmmApartments
													.getString("sourceID")
													: null);

									propFeeApartmentService
											.addPropFeeApartment(propFeeApartment);

									JSONArray owners = propertyInfo
											.getJSONObject(i).getJSONArray(
													"owners");

									for (int j = 0; j < owners.size(); j++) {
										JSONObject owner = owners
												.getJSONObject(j)
												.getJSONObject("owner");

										List<CerHouseholderDto> cerHouseholderDtos = cerHouseholderService
												.findCerHouseholderByRelevantID(
														null,
														propFeeApartment
																.getApartmentID(),
														"0",
														null,
														owner.getString("name"),
														owner.getString("phone"),
														null);

										if (cerHouseholderDtos != null
												&& cerHouseholderDtos.size() > 0) {
											continue;
										}

										/***
										 * 添加业主关系
										 */

										CerHouseholderDto cerHouseholder = new CerHouseholderDto();
										cerHouseholder.setName(owner
												.getString("name"));
										cerHouseholder.setPhone(owner
												.getString("phone"));
										cerHouseholder
												.setApartmentID(propFeeApartment
														.getApartmentID());
										JSONObject linkPropertyHouseholder = owners
												.getJSONObject(j)
												.getJSONObject(
														"linkPropertyHouseholder");
										cerHouseholder
												.setType(linkPropertyHouseholder
														.getString("type"));
										cerHouseholder
												.setLinkAptHouseholderID(linkPropertyHouseholder
														.getString("linkAptHouseholderID"));
										cerHouseholderService
												.addCerHouseholder(cerHouseholder);
									}

								}

							}

						}

						/***
						 * 修改产权
						 */
						if (receiver.get("receiver").toString()
								.equals("RECEIVER_EDIT_PROPERTY")
								&& messageType.equals("RECEIVER_EDIT_PROPERTY")) {

							PropFeeApartmentDto propFeeApartment = new PropFeeApartmentDto();

							/***
							 * 修改产权信息
							 */

							JSONObject cmmApartments = JSONObject
									.fromObject(paMap.get("apartments")
											.toString());

							propFeeApartment.setApartmentID(cmmApartments
									.get("apartmentID") != null ? cmmApartments
									.getString("apartmentID") : null);

							propFeeApartment.setFeeArea(cmmApartments
									.get("feeArea") != null ? cmmApartments
									.getString("feeArea") : null);

							propFeeApartment
									.setBuildingArea(cmmApartments
											.get("buildingArea") != null ? cmmApartments
											.getString("buildingArea") : null);

							propFeeApartment
									.setPropertyArea(cmmApartments
											.get("propertyArea") != null ? cmmApartments
											.getString("propertyArea") : null);
							propFeeApartment.setFloor(cmmApartments
									.get("floor") != null ? cmmApartments
									.getString("floor") : null);
							propFeeApartment.setIsDeliver(cmmApartments
									.get("isDeliver") != null ? cmmApartments
									.getString("isDeliver") : null);

							propFeeApartment.setIsOccupy(cmmApartments
									.get("isOccupy") != null ? cmmApartments
									.getString("isOccupy") : null);

							propFeeApartmentService
									.editPropFeeApartment(propFeeApartment);
						}
						/***
						 * 导入产权
						 */
						if (receiver.get("receiver").toString()
								.equals("RECEIVER_IMPORT_PROPERTY")
								&& messageType
										.equals("RECEIVER_IMPORT_PROPERTY")) {

							if (paMap.containsKey("houseInfo")) {
								JSONObject propertyInfo = JSONObject
										.fromObject(paMap.get("houseInfo")
												.toString());

								PropFeeBuildingDto propFeeBuilding = new PropFeeBuildingDto();
								PropFeeUnitDto propFeeUnit = new PropFeeUnitDto();
								PropFeeApartmentDto propFeeApartment = new PropFeeApartmentDto();

								/***
								 * 添加楼栋
								 */
								JSONObject cmmBuilding = propertyInfo
										.getJSONObject("cmmBuilding");
								propFeeBuilding
										.setBuildingID(cmmBuilding
												.get("buildingID") != null ? cmmBuilding
												.getString("buildingID") : null);
								propFeeBuilding
										.setCommID(cmmBuilding
												.getString("commID") != null ? cmmBuilding
												.getString("commID") : null);

								propFeeBuilding
										.setBuildingName(cmmBuilding
												.get("buildingName") != null ? cmmBuilding
												.getString("buildingName")
												: null);
								propFeeBuilding
										.setBuildingNameOther(cmmBuilding
												.get("buildingNameOther") != null ? cmmBuilding
												.getString("buildingNameOther")
												: null);

								propFeeBuilding.setBuildingSort(cmmBuilding
										.get("buildingSort") != null ? Integer
										.parseInt(cmmBuilding
												.getString("buildingSort"))
										: null);

								propFeeBuilding
										.setBuildingType(cmmBuilding
												.get("buildingType") != null ? cmmBuilding
												.getString("buildingType")
												: null);

								propFeeBuilding.setStatus(cmmBuilding
										.get("status") != null ? cmmBuilding
										.getString("status") : null);

								Map<String, Object> buildMap = new HashMap<>();
								buildMap.put("buildingName",
										propFeeBuilding.getBuildingName());
								buildMap.put("commID",
										propFeeBuilding.getCommID());
								List<Map<String, Object>> buildings = propFeeBuildingService
										.bauthfindPropFeeBuildingByMap(buildMap);

								if (buildings == null || buildings.size() <= 0) {
									propFeeBuildingService
											.addPropFeeBuilding(propFeeBuilding);
								}

								/***
								 * 添加单元
								 */
								JSONObject cmmUnit = propertyInfo
										.getJSONObject("cmmUnit");

								propFeeUnit.setBuildingID(cmmUnit
										.get("buildingID") != null ? cmmUnit
										.getString("buildingID") : null);
								propFeeUnit
										.setUnitID(cmmUnit.get("unitID") != null ? cmmUnit
												.getString("unitID") : null);
								propFeeUnit
										.setUnitName(cmmUnit
												.getString("unitName") != null ? cmmUnit
												.getString("unitName") : null);

								Map<String, Object> unitPa = new HashMap<>();
								unitPa.put("unitName",
										propFeeUnit.getUnitName());
								unitPa.put("buildingID",
										propFeeUnit.getBuildingID());
								List<Map<String, Object>> units = propFeeUnitService
										.bauthfindPropFeeUnitByMap(unitPa);
								if (units == null || units.size() <= 0) {
									propFeeUnitService
											.addPropFeeUnit(propFeeUnit);
								}

								/***
								 * 添加房屋
								 */

								JSONObject cmmApartments = propertyInfo
										.getJSONObject("cmmApartments");

								propFeeApartment
										.setApartmentID(cmmApartments
												.get("apartmentID") != null ? cmmApartments
												.getString("apartmentID")
												: null);

								propFeeApartment
										.setApartmentNum(cmmApartments
												.get("apartmentNumb") != null ? cmmApartments
												.getString("apartmentNumb")
												: null);

								propFeeApartment
										.setApartmentType(cmmApartments
												.get("apartmentType") != null ? cmmApartments
												.getString("apartmentType")
												: null);

								propFeeApartment
										.setBuildingID(cmmApartments
												.get("buildingID") != null ? cmmApartments
												.getString("buildingID") : null);

								propFeeApartment.setFeeArea(cmmApartments
										.get("feeArea") != null ? cmmApartments
										.getString("feeArea") : null);

								propFeeApartment
										.setBuildingArea(cmmApartments
												.get("buildingArea") != null ? cmmApartments
												.getString("buildingArea")
												: null);

								propFeeApartment
										.setPropertyArea(cmmApartments
												.get("propertyArea") != null ? cmmApartments
												.getString("propertyArea")
												: null);
								propFeeApartment
										.setFloor(cmmApartments
												.getString("floor") != null ? cmmApartments
												.getString("floor") : null);
								propFeeApartment
										.setIsDeliver(cmmApartments
												.get("isDeliver") != null ? cmmApartments
												.getString("isDeliver") : null);

								propFeeApartment
										.setIsOccupy(cmmApartments
												.get("isOccupy") != null ? cmmApartments
												.getString("isOccupy") : null);
								propFeeApartment.setUnitID(cmmApartments
										.getString("unitID"));
								propFeeApartment
										.setSourceID(cmmApartments
												.get("sourceID") != null ? cmmApartments
												.getString("sourceID") : null);

								Map<String, Object> apartmentsPa = new HashMap<>();
								apartmentsPa.put("apartmentNum",
										propFeeApartment.getApartmentNum());
								// map2.put("floorID",
								// cmmApartmentsDto.getFloorID());
								apartmentsPa.put("buildingID",
										propFeeApartment.getBuildingID());
								List<Map<String, Object>> apartments = propFeeApartmentService
										.bauthfindPropFeeApartmentByMap(apartmentsPa);
								if (apartments == null
										|| apartments.size() <= 0) {
									propFeeApartmentService
											.addPropFeeApartment(propFeeApartment);
								}
								JSONArray owners = propertyInfo
										.getJSONArray("owners");
								for (int j = 0; j < owners.size(); j++) {
									JSONObject owner = owners.getJSONObject(j)
											.getJSONObject("owner");

									List<CerHouseholderDto> cerHouseholderDtos = cerHouseholderService
											.findCerHouseholderByRelevantID(
													null, propFeeApartment
															.getApartmentID(),
													"0", null, owner
															.getString("name"),
													owner.getString("phone"),
													null);

									if (cerHouseholderDtos != null
											&& cerHouseholderDtos.size() > 0) {
										continue;
									}

									/***
									 * 添加业主关系
									 */

									CerHouseholderDto cerHouseholder = new CerHouseholderDto();
									cerHouseholder.setName(owner
											.getString("name"));
									cerHouseholder.setPhone(owner
											.getString("phone"));
									cerHouseholder
											.setApartmentID(propFeeApartment
													.getApartmentID());
									JSONObject linkPropertyHouseholder = owners
											.getJSONObject(j).getJSONObject(
													"linkPropertyHouseholder");
									cerHouseholder
											.setType(linkPropertyHouseholder
													.getString("type"));
									cerHouseholder
											.setLinkAptHouseholderID(linkPropertyHouseholder
													.getString("linkAptHouseholderID"));
									cerHouseholderService
											.addCerHouseholder(cerHouseholder);
								}

							}

						}
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}

	}
}
